#include <iostream>
#include <limits>
using std::cout;
using std::cin;
using std::endl;
using std::ostream;
using std::istream;

void InputInt(istream & is,int & num){
    while(is >> num, !is.eof()){
        if(is.bad()){
            cout << "istream has broken" << endl;
            return;
        }else if(is.fail()){
            is.clear();
            is.ignore(std::numeric_limits<std::streamsize>::max(),
                      '\n');
            cout << "请输入一个整型数据！！！！" << endl;
        }else{
            break;
        }
    }
}



class Complex
{
public:
    Complex(int real,int image)
    : _real(real)
    , _image(image)
    {}

    void print() const{
        cout << _real << " + " << _image << "i" << endl;
    }

    Complex & operator+=(const Complex & rhs){
        _real += rhs._real;
        _image += rhs._image;
        return *this;
    }

    
    Complex & operator++(){
        cout << "前置++重载函数" << endl;
        ++_real;
        ++_image;
        return *this;
    }

    Complex operator++(int){
        cout << "后置++重载函数" << endl;
        Complex temp(*this);
        ++_real;
        ++_image;
        return temp;
    }

    friend
    Complex operator+(const Complex & lhs,const Complex & rhs);
    friend
    ostream & operator<<(ostream & os,const Complex & rhs);
    friend
    istream & operator>>(istream & is,Complex & rhs);
private:
    int _real;
    int _image;
};

Complex operator+(const Complex & lhs,const Complex & rhs){
    return Complex(lhs._real + rhs._real, lhs._image + rhs._image);
}

ostream & operator<<(ostream & os,const Complex & rhs){
    os << rhs._real << " + " << rhs._image << "i";
    return os;
}

istream & operator>>(istream & is,Complex & rhs){
    cout << "请输入实部：" << endl;
    /* is >> rhs._real; */
    InputInt(is,rhs._real);
    cout << "请输入虚部：" << endl;
    /* is >> rhs._image; */
    InputInt(is,rhs._image);
    return is;
}


void test0(){
    Complex cx1(1,2);
    Complex cx2(3,4);
    Complex cx3 = cx1 + cx2;
    /* cx3.print(); */
    
    /* cx3.operator<<(cout); */
    /* cx3 << cout; */

    /* operator<<(cout,cx3); */
    /* cout << cx3; */
    cout << cx3 << endl;
    cin >> cx3;
    cout << cx3 << endl;
}



int main(void){
    test0();
    return 0;
}
